<?php
// index.php?mod=home&childmod=settings&ajax=true&op=updateBlobHeadImage
$op = get_get("op");
if ($op == "") {
    returnMessage("-1", "unknownrequest");
}
if ($op == "updatehomesetting") {
    if (! isLogined()) {
        returnMessage(- 1, "你没有登陆");
    }
    $uid = getSessionUid();
    $allow_commit = get_post("allow_commit");
    if (! ($allow_commit == 0 || $allow_commit == 1))
        returnMessage(- 2, "allow_commit 不合法");

    $allow_leave_message = get_post("allow_leave_message");
    if (! ($allow_leave_message == 0 || $allow_leave_message == 1))
        returnMessage(- 2, "allow_leave_message 不合法");
    $ban_list = get_post("ban_list");
    if (! $ban_list)
        $ban_list = array();

    $display_in_profile_card = get_post("display_in_profile_card");
    if (! $display_in_profile_card)
        $display_in_profile_card = array();

    updateSetting($uid, $display_in_profile_card, $ban_list, $allow_leave_message, $allow_commit);
    returnMessage(0, "更改成功");
}
if ($op == "updateprofile") {
    if (! isLogined()) {
        returnMessage(- 1, "你没有登陆");
    }

    $uid = getSessionUid();

    $name = get_post("name");
    if ($name == "")
        returnMessage(- 1, "姓名不能为空");

    $gender = get_post("gender");
    if (! ($gender == 0 || $gender == 1))
        returnMessage(- 2, "性别不合法");

    $user_type = get_post("user_type");
    if (! ($user_type == 0 || $user_type == 1 || $user_type == 2))
        returnMessage(- 3, "用户身份不合法");
    $birthday = get_post("birthday");
    if ($birthday == "")
        returnMessage(- 6, "生日不能为空");
    $introduce = get_post("introduce");
    if ($introduce == "")
        returnMessage(- 4, "个人介绍不能为空");

    $motto = get_post("motto");
    if ($motto == "")
        returnMessage(- 5, "座右铭不能为空");
    global $_G;
    if (! isset($_G["profile"])) {
        createProfile($uid, $name, $user_type, $introduce, $birthday, $gender, $motto);
        returnMessage(0, "用户档案创建成功，欢迎你来到学生云。");
    } else {
        updateProfile($uid, $name, $user_type, $introduce, $birthday, $gender, $motto);
        returnMessage(0, "更改成功");
    }
}
if ($op == "updateheadimage") {
    if (count($_FILES) != 1) {
        returnMessage("-7", "上传头像只能上传一个");
    }
    $data = onFileUpdate($_FILES["file"]["name"][0], $_FILES["file"]["tmp_name"][0], $_FILES["file"]["size"][0]);
    if ($data) {
        $rid = $data["rid"];
        $uid = getSessionUid();
        updateHeadImage($uid, $rid);
        returnMessage(0, "上传成功，请刷新后查看效果", getHeadImageURL());
    } else {
        returnMessage("-18", "头像更新失败，数据库上传失败，请联系管理员");
    }
}
if ($op == "class_apply") {
    $uid = getSessionUid();
    $profile = getProfile($uid);
    if (! $profile)
        returnMessage(- 1, "请完善资料后再来申请");
    if ($profile["user_type"] != 0)
        returnMessage(- 2, "只有学生可以申请");
    $classid = get_post("classid");
    if (! $classid)
        returnMessage(- 3, "未填写classid");
    $class = getClass($classid);
    if (! $class)
        returnMessage(- 4, "班级未找到");
    if ($class["enable"] == 0)
        returnMessage(- 5, "班级已关闭，不接受新学生申请");
    if (getApplyData($uid))
        returnMessage(- 6, "你已经发送过了申请，请等待上一个申请被批复");
    $note = get_post("note");
    apply_class($uid, $note, $classid);
    sendNotice($uid, "您刚刚向我们发送的" . $class["displayname"] . "申请已成功接收，请等待相关班级班主任或管理员进行批复.");
    returnMessage(0, "申请成功，请留意您的应用提醒，审核结果将会从那里通知您", array(
        "op" => "showDetial",
        "applydata" => getApplyData($uid)
    ));
}

if ($op == "updateBlobHeadImage") {
    if (count($_FILES) != 1) {
        returnMessage("-7", "上传头像只能上传一个");
    }
    $data = onFileUpdate($_FILES["file"]["name"], $_FILES["file"]["tmp_name"], $_FILES["file"]["size"]);
    if ($data) {
        $rid = $data["rid"];
        $uid = getSessionUid();
        updateHeadImage($uid, $rid);
        returnMessage(0, "上传成功，请刷新后查看效果", getHeadImageURL());
    } else {
        returnMessage("-18", "头像更新失败，数据库上传失败，请联系管理员");
    }
}
if ($op == "settings_profile") {
    $data = array();
    global $_G;
    $profile = $_G["profile"];
    if (! $profile) {
        $data["new"] = true;
    } else {
        $data["new"] = false;
    }
    $data["profile"] = $profile;
    setSetting("profile_settings", $data);
    loadTemp("home/settings_view/settings_profile.php");
    die();
}
if ($op == "class_data") {
    global $_G;
    $uid = getSessionUid();
    $profile = getProfile($uid);
    if (! $profile) {
        printMessage("error", "仅注册用户可以访问，您的存档不全请先完善");
        die();
    }
    if ($profile["user_type"] != 0) {
        printMessage("error", "仅学生可以访问这里");
        die();
    }
    $setting = array();
    $applydata = getApplyData($uid);
    if ($applydata) {
        $setting["op"] = 1;
        $setting["applydata"] = $applydata;
    } else {
        if (is_Student($uid)) {
            $setting["op"] = 2;
            $class = getClassByStudentUid($uid);
            $setting["classdata"] = $class;
            $setting["master"] = getCardInfo($class["master"]);
            $setting["students"] = getStudents($class["classid"]);
        } else {
            $setting["op"] = 3;
            $setting["classlist"] = getEnabledClassList();
        }
    }
    setSetting("class_data_settings", $setting);
    loadTemp("home/settings_view/settings_classdata.php");
    die();
}
if ($op == "setting_head_image") {
    global $_G;
    $data = array();
    $data["hasImage"] = isset($_G["profile"]["head_image"]) && $_G["profile"]["head_image"] != - 1;
    $data["imageurl"] = getHeadImageURL();
    setSetting("head_image_settings", $data);
    loadTemp("home/settings_view/settings_head_image.php");
    die();
}

if ($op == "setting_home") {
    global $data;
    $data = getDetialList(getSessionUid());
    setSetting("home_settings", $data);
    loadTemp("home/settings_view/settings_home.php");
    die();
}
if ($op == "search_for_ban_list") {
    $account = get_post("account");
    if (! $account) {
        returnMessage(1, "你没有填写账户");
    }
    $return = getCardInfoByLoginData($account);
    if (! $return) {
        returnMessage(2, "用户不存在");
    }
    if ($return["uid"] == getSessionUid()) {
        returnMessage(3, "你不能禁用你自己");
    }
    returnMessage(0, "搜索成功，已经添加到禁用列表", $return);
}
if ($op == "setting_password") {
    loadTemp("home/settings_view/settings_password.php");
    die();
}

if ($op == "setting_private") {
    loadTemp("home/settings_view/settings_private.php");
    die();
}

if ($op == "setting_parent") {
    $uid = getSessionUid();
    $profile = getProfile($uid);
    if ($profile["user_type"] == 0) {
        printMessage("error", "学生不能进入此页面，如果您认为您的账户类型有误，请联系班主任进行修改。");
        die();
    }
    $setting = array();
    $relationships = getChildren($uid);
    $relations = array();
    if ($relationships) {
        foreach ($relationships as $v) {
            $cardinfo = getCardInfo($v["childid"]);
            $relations[$v["childid"]]["uid"] = $v["childid"];
            $relations[$v["childid"]]["relationship"] = $v["relationship"];
            $relations[$v["childid"]]["data"] = $cardinfo;
        }
    }
    $setting["relations"] = $relations;
    setSetting("relationship_settings", $setting);
    loadTemp("home/settings_view/settings_parent.php");
    die();
}
if ($op == "updaterelationship") {

    $uid = getSessionUid();
    $profile = getProfile($uid);
    if ($profile["user_type"] == 0) {
        returnMessage(- 10, "学生没有权限作为别人的父母");
    }
    $account = get_post("account");
    if (! $account)
        returnMessage(- 10, "账号不能为空");
    $passwd = get_post("passwd");
    if (! $passwd)
        returnMessage(- 10, "密码不能为空");
    $relationship = get_post("relationship");
    if (! $relationship)
        returnMessage(- 10, "称呼不能为空");
    $code = addChild($account, $passwd, $uid, $relationship);

    if ($code == 0) {
        $childid = getUID($account);
        $cardinfo = getCardInfo($childid);
        $data = array();
        $data["uid"] = $childid;
        $data["relationship"] = $relationship;
        $data["data"] = $cardinfo;
        returnMessage(0, "添加成功", $data);
    }
    if ($code == - 10)
        returnMessage(- 10, "您的用户存档不存在，请联系管理员");

    if ($code == - 9)
        returnMessage(- 9, "学生没有权限作为别人的父母");

    if ($code == - 8)
        returnMessage(- 8, "TA已经被记录为您的子女了");

    if ($code == - 1)
        returnMessage(- 1, "您寻找的账户不存在");

    if ($code == - 2)
        returnMessage(- 2, "密码错误，请重试");

    if ($code == - 11)
        returnMessage(- 11, "您不能添加教职工或其他家长为自己的子女");

    returnMessage(- 101, "未知错误，请联系管理员");
}

if ($op == "admin_panel") {
    loadTemp("home/settings_view/admin_panel.php");
    die();
}
if ($op == "pwchange") {
    $oldpwd = get_post("oldpwd");
    if (judzePassword(getSessionUid(), $oldpwd, getSessionUid(), true) != 2) {
        returnMessage(- 1, "原密码填写错误，请重新核验，如有异议请咨询站点管理员");
    }
    $newpwd = get_post("newpwd");
    if (! $newpwd) {
        returnMessage(- 2, "您未填写新密码");
    }
    if (! chengePasswd(getSessionUid(), $newpwd)) {
        returnMessage(- 3, "数据库错误，请联系管理员");
    } else {
        returnMessage(0, "修改成功，您的设置已生效，请重新登录 ");
        removeSessionFileByUid(getSessionUid());
    }
}

returnMessage("-2", "unknownopreaterequest: $op");
?>